//
//  PersonalInfoViewController.m
//  IpadTestB
//
//  Created by edianzu on 2017/6/29.
//  Copyright © 2017年 ruilai. All rights reserved.
//

#import "PersonalInfoViewController.h"
#import "PersonalPlanCell.h"
#import "EditBabyView.h"
#import "PersonDataViewController.h"
@interface PersonalInfoViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,UIImagePickerControllerDelegate,SDDatePickerViewProtocol>
@property(nonatomic,strong)PlanModel * planModel;
@property(nonatomic,strong)EditBabyView * editBabyView;
@property(nonatomic,strong)BaseAlertView * alertView;
@property(nonatomic,strong)NSMutableDictionary * allDataDictionary;
@property (nonatomic,strong) NSMutableArray * joinNumberArray;
@end

@implementation PersonalInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"个人信息";
    self.view.backgroundColor = UIColorFromRGB(241, 246, 248);
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont boldSystemFontOfSize:23*AutoScaleHorizontal],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    ////////////////左按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBtn.frame = CGRectMake(0, 0, 35,35);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"navBack"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBarBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];;
    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //将宽度设为负值
    spaceItem.width = 15;
    //将两个BarButtonItem都返回给NavigationItem
    self.navigationItem.leftBarButtonItems = @[spaceItem,leftBarBtn];
    
    //创建右边按钮
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 140,35);
//    rightBtn.layer.cornerRadius =10;
    rightBtn.backgroundColor = [UIColor clearColor];
    //    [rightBtn setImage:[UIImage imageNamed:@"navRightBtn"] forState:UIControlStateNormal];
    [rightBtn setTitle:@"删除" forState:UIControlStateNormal];
    [rightBtn setImage:[UIImage imageNamed:@"deleteChild"] forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:20*AutoScaleHorizontal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBarBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightBarBtn = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];;
    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItemm = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //将宽度设为负值
    spaceItemm.width = -5;
    //将两个BarButtonItem都返回给NavigationItem
    self.navigationItem.rightBarButtonItems = @[spaceItemm,rightBarBtn];
    
    NSLog(@"%@",self.model.ChildName);
//    [self getData];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getData];
}
-(void)getData{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    self.planArray = [[NSMutableArray alloc]init];
    self.schoolNameArr = [[NSMutableArray alloc]initWithCapacity:0];
    self.joinNumberArray = [[NSMutableArray alloc]init];
    //获取全部数据
    NSString* str = [user objectForKey:@"AllData"];
    //    NSLog(@"eee===%@",str);
    NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
    _allDataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    NSMutableArray * allClassArr  = [[NSMutableArray alloc]init];
    allClassArr = [_allDataDictionary objectForKey:@"ClassList"];
    for (NSDictionary * dic in allClassArr) {
        ClassModel * mod = [ClassModel modelWithDictionary:dic];
        [self.schoolNameArr addObject:mod.Name];
    }
    
    NSMutableArray * joinedPlanArr = [[NSMutableArray alloc]init];//把孩子参加的计划ID 储存起来一会儿遍历
    NSMutableArray * testArray = [_allDataDictionary objectForKey:@"TestResult"];
    for (NSMutableDictionary * dict in testArray) {
        NSMutableDictionary * masterDic = [dict objectForKey:@"master"];
        if ([[masterDic objectForKey:@"ChildId"]isEqual:self.model.Id]) {
            [joinedPlanArr addObject:[masterDic objectForKey:@"ProgramId"]];
        }
    }
    
    //遍历拿到参加的计划模型
    NSMutableArray * planArray = [_allDataDictionary objectForKey:@"Programs"];
    for (NSNumber * num in joinedPlanArr) {
        for (NSMutableDictionary * dict in planArray) {
            if ([[dict objectForKey:@"Id"]isEqual:num]) {
                PlanModel * pmodel = [PlanModel modelWithDictionary:dict];
                [self.planArray addObject:pmodel];
            }
        }
    }
    
    //根据创建时间倒序排列
    if (self.planArray.count!=0) {
        
    for (int  i =0; i<[self.planArray count]-1; i++) {
        
        for (int j = i+1; j<[self.planArray count]; j++) {
            
            PlanModel * model = self.planArray[i];
            NSString * birthStr = [model.CreateTime substringWithRange:NSMakeRange(0, 10)];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            NSDate *birthDate = [dateFormatter dateFromString:birthStr];
            NSString * realAge = [self dateToDetailOld:birthDate];
            
            
            PlanModel * model1 = self.planArray[j];
            NSString * birthStr1 = [model1.CreateTime substringWithRange:NSMakeRange(0, 10)];
            NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
            [dateFormatter1 setDateFormat:@"yyyy-MM-dd"];
            NSDate *birthDate1 = [dateFormatter1 dateFromString:birthStr1];
            NSString * realAge1 = [self dateToDetailOld:birthDate1];
            
            if ([realAge intValue]>[realAge1 intValue]) {
                
                //交换
                
                [self.planArray exchangeObjectAtIndex:i withObjectAtIndex:j];
                
            }
            
        }
        
    }
    }

    
    //循环遍历看有多少人参加了现有的计划
//    NSMutableArray * testArray = [NSMutableArray arrayWithArray: [_allDataDictionary objectForKey:@"TestResult"]];
    NSMutableArray * childArray = [NSMutableArray arrayWithArray:[_allDataDictionary objectForKey:@"ChildList"]];
    for (NSNumber * num in joinedPlanArr) {
        NSInteger joinProNumber = 0;
//        for (NSMutableDictionary * childDic in childArray) {
        
            for (NSDictionary * dict in testArray) {
                NSDictionary * masterdic = [dict objectForKey:@"master"];
                
//                if ([[childDic objectForKey:@"Id"]isEqual:[masterdic objectForKey:@"ChildId"]]) {
                
                    if ([[masterdic objectForKey:@"ProgramId"]isEqual:num]) {
                        joinProNumber += 1;
                    }
                }
//            }
        
//        }
        NSNumber * joinNum = [NSNumber numberWithInt:joinProNumber];
        [self.joinNumberArray addObject:joinNum];
    }
    [user synchronize];
    [self setUI];
}

//创建视图
-(void)setUI{
    //整个依赖SCRVIEW
    //整个依赖scrollview
    _myScrollView = [[UIScrollView alloc]init];
    _myScrollView.backgroundColor = UIColorFromRGB(241, 246, 248);
    _myScrollView.contentSize = CGSizeMake(0, 1000);
    _myScrollView.scrollEnabled =YES;
    _myScrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_myScrollView];
    [_myScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left);
        make.top.mas_equalTo(self.view.mas_top);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT));
    }];
    
    //上方白色背景
    self.topView = [[UIView alloc]init];
    self.topView.backgroundColor = [UIColor whiteColor];
    self.topView.layer.cornerRadius = 10;
    self.topView.layer.shadowColor=[[UIColor grayColor] colorWithAlphaComponent:0.8].CGColor;
    self.topView.layer.shadowOffset=CGSizeMake(10,10);
    self.topView.layer.shadowOpacity=0.5;
    self.topView.layer.shadowRadius=8;
    self.topView.layer.masksToBounds = YES;
    [self.myScrollView addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.myScrollView.mas_left).with.offset(75*AutoScaleHorizontal);
//        make.right.mas_equalTo(self.myScrollView.mas_right).with.offset(-75*AutoScaleHorizontal);
        make.top.mas_equalTo(self.myScrollView.mas_top).with.offset(25*AutoScaleVertical);
        make.height.mas_equalTo(210*AutoScaleVertical);
        make.width.mas_equalTo(862*AutoScaleHorizontal);
    }];
    //姓名
    self.topNameLabel = [[UILabel alloc]init];
    self.topNameLabel.font = [UIFont boldSystemFontOfSize:25*AutoScaleHorizontal];
    self.topNameLabel.textColor = [UIColor blackColor];
    self.topNameLabel.text = self.model.ChildName;
//    NSDictionary *attrs = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:25 * AutoScaleHorizontal]};
//    CGSize size=[self.model.ChildName sizeWithAttributes:attrs];
    CGSize maximumLabelSize = CGSizeMake(100, 9999);//labelsize的最大值
         //关键语句
    CGSize expectSize = [self.topNameLabel sizeThatFits:maximumLabelSize];
        [self.myScrollView addSubview:self.topNameLabel];
    
    [self.topNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.topView.mas_left).with.offset(45*AutoScaleHorizontal);
        make.top.mas_equalTo(self.topView.mas_top).with.offset(30*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(expectSize.width*AutoScaleHorizontal, 30*AutoScaleVertical));
    }];
    //性别
    self.genderIMG = [[UIImageView alloc]init];
    if ([self.model.Gender isEqual:@1]) {
        self.genderIMG.image = [UIImage imageNamed:@"manIMGS"];
    }else{
        self.genderIMG.image = [UIImage imageNamed:@"womenIMGS"];
    }
    [self.topView addSubview:self.genderIMG];
    [self.genderIMG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.topNameLabel.mas_right).with.offset(20*AutoScaleHorizontal);
        make.centerY.mas_equalTo(self.topNameLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(22*AutoScaleHorizontal, 22*AutoScaleVertical));
    }];
    
    //班级
    self.classLabel = [[UILabel alloc]init];
    self.classLabel.font = [UIFont boldSystemFontOfSize:20*AutoScaleHorizontal];
    self.classLabel.textColor = [UIColor blackColor];
    self.classLabel.text = @"所属班级:";
    NSDictionary *attr = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:25*AutoScaleHorizontal]};
    CGSize sizee=[self.classLabel.text sizeWithAttributes:attr];
    [self.myScrollView addSubview:self.classLabel];
    [self.classLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.topView.mas_left).with.offset(45*AutoScaleHorizontal);
        make.centerY.mas_equalTo(self.topView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(sizee.width, 25*AutoScaleVertical));
    }];
    
    self.classNameLabel = [[UILabel alloc]init];
    self.classNameLabel.font = [UIFont boldSystemFontOfSize:20*AutoScaleHorizontal];
    self.classNameLabel.textColor = [UIColor blackColor];
    self.classNameLabel.text = self.model.Name;
    [self.myScrollView addSubview:self.classNameLabel];
    [self.classNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.classLabel.mas_right);
        make.centerY.mas_equalTo(self.topView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(200*AutoScaleHorizontal, 25*AutoScaleVertical));
    }];

    
    
    //生日
    self.birthLabel = [[UILabel alloc]init];
    self.birthLabel.font = [UIFont boldSystemFontOfSize:20*AutoScaleHorizontal];
    self.birthLabel.textColor = [UIColor blackColor];
    self.birthLabel.text = @"生日:";
    NSDictionary *attr2 = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:25*AutoScaleHorizontal]};
    CGSize sizee2=[self.birthLabel.text sizeWithAttributes:attr2];
    [self.myScrollView addSubview:self.birthLabel];
    [self.birthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.topView.mas_left).with.offset(320*AutoScaleHorizontal);
        make.centerY.mas_equalTo(self.topView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(sizee2.width, 25*AutoScaleVertical));
    }];
    
    self.birthNameLabel = [[UILabel alloc]init];
    self.birthNameLabel.text = [self.model.Birthday substringWithRange:NSMakeRange(0, 10)];;
    self.birthNameLabel.font = [UIFont boldSystemFontOfSize:20*AutoScaleHorizontal];
    self.birthNameLabel.textColor = [UIColor blackColor];
    [self.myScrollView addSubview:self.birthNameLabel];
    [self.birthNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.birthLabel.mas_right);
        make.centerY.mas_equalTo(self.topView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(130*AutoScaleHorizontal, 25*AutoScaleVertical));
    }];
    //家长电话
    self.phoneLabel = [[UILabel alloc]init];
    self.phoneLabel.font = [UIFont boldSystemFontOfSize:20*AutoScaleHorizontal];
    self.phoneLabel.textColor = [UIColor blackColor];
    self.phoneLabel.text = @"家长电话:";
    NSDictionary *attr25 = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:25*AutoScaleHorizontal]};
    CGSize sizee25=[self.phoneLabel.text sizeWithAttributes:attr25];
    [self.myScrollView addSubview:self.phoneLabel];
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.topView.mas_left).with.offset(550*AutoScaleHorizontal);
        make.centerY.mas_equalTo(self.topView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(sizee25.width, 25*AutoScaleVertical));
        
    }];
    
    self.phoneNameLabel = [[UILabel alloc]init];
    if (self.model.Phone == nil || self.model.Phone.length == 0) {
        
        self.phoneNameLabel.text = @"无";
    }else{
        self.phoneNameLabel.text = self.model.Phone;
    }
    self.phoneNameLabel.font = [UIFont boldSystemFontOfSize:20*AutoScaleHorizontal];
    self.phoneNameLabel.textColor = [UIColor blackColor];
    
    [self.myScrollView addSubview:self.phoneNameLabel];
    [self.phoneNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.phoneLabel.mas_right);
        make.centerY.mas_equalTo(self.topView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(150*AutoScaleHorizontal, 25*AutoScaleVertical));
    }];

    //负责教师
    self.responselabel = [[UILabel alloc]init];
    self.responselabel.font = [UIFont boldSystemFontOfSize:20*AutoScaleHorizontal];
    self.responselabel.textColor = [UIColor blackColor];
    self.responselabel.text = @"负责教师:";
    NSDictionary *attr3 = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:25*AutoScaleHorizontal]};
    CGSize sizee3=[self.responselabel.text sizeWithAttributes:attr3];
    [self.myScrollView addSubview:self.responselabel];
    [self.responselabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.topView.mas_left).with.offset(45*AutoScaleHorizontal);
        make.top.mas_equalTo(self.birthNameLabel.mas_bottom).with.offset(10*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(sizee3.width, 25*AutoScaleVertical));
    }];
    
    self.responseTeacher = [[UILabel alloc]init];
    self.responseTeacher.font = [UIFont boldSystemFontOfSize:20*AutoScaleHorizontal];
    self.responseTeacher.textColor = [UIColor blackColor];
    if (self.model.ResponsibleTeacher !=nil) {
        self.responseTeacher.text = self.model.ResponsibleTeacher;
    }else{
        self.responseTeacher.text = @"无负责教师";
    }
    [self.myScrollView addSubview:self.responseTeacher];
    [self.responseTeacher mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.responselabel.mas_right);
        make.centerY.mas_equalTo(self.responselabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(150*AutoScaleHorizontal, 25*AutoScaleVertical));
    }];
    //编辑资料
    self.editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.editBtn setTitle:@"编辑资料" forState:UIControlStateNormal];
    self.editBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20*AutoScaleHorizontal];
    [self.editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.editBtn.backgroundColor = UIColorFromRGB(26, 167, 157);
    self.editBtn.layer.cornerRadius = 10;
    self.editBtn.layer.masksToBounds =YES;
    [self.editBtn addTarget:self action:@selector(pressEdit) forControlEvents:UIControlEventTouchUpInside];
    [self.myScrollView addSubview:self.editBtn];
    [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.topView.mas_centerX);
        make.bottom.mas_equalTo(self.topView.mas_bottom).with.offset(-15*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(110*AutoScaleHorizontal, 30*AutoScaleVertical));
    }];
    //中间参加的计划
    self.joinLabel = [[UILabel alloc]init];
    self.joinLabel.text = @"参加的测试计划";
    self.joinLabel.textColor = [UIColor blackColor];
    self.joinLabel.font = [UIFont boldSystemFontOfSize:20*AutoScaleHorizontal];
    [self.myScrollView addSubview:self.joinLabel];
    [self.joinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.topView.mas_left);
        make.top.mas_equalTo(self.topView.mas_bottom).with.offset(35*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(150*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    self.lineView = [[UIView alloc]init];
    self.lineView.backgroundColor = UIColorFromRGB(216, 133, 119);
    [self.myScrollView addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.joinLabel.mas_centerY);
        make.left.mas_equalTo(self.joinLabel.mas_right).with.offset(30*AutoScaleHorizontal);
        make.height.mas_equalTo(1);
        make.right.mas_equalTo(self.topView.mas_right);
    }];
    //创建列表
    //有数据时创建列表
    self.myTableView = [[UITableView alloc]init];
    self.myTableView.backgroundColor =UIColorFromRGB(241, 244, 248);
    self.myTableView.delegate =self;
    self.myTableView.dataSource =self;
    self.myTableView.showsVerticalScrollIndicator =NO;
    //无分割线
        self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //多余的CELL 隐藏
    self.myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.myTableView registerClass:[PersonalPlanCell class] forCellReuseIdentifier:@"PersonalPlanCell"];
    [self.myScrollView addSubview:self.myTableView];
    
    NSInteger manyHeight = 0;
    
        for (PlanModel * mod in self.planArray) {
            manyHeight +=1;
        }
    
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.joinLabel.mas_bottom).with.offset(20*AutoScaleVertical);
        make.left.mas_equalTo(self.joinLabel.mas_left);
        make.right.mas_equalTo(self.topView.mas_right);
        make.height.mas_equalTo(85 * manyHeight * AutoScaleVertical);
    }];
    //SCROLLVIEW 的偏移量
    self.myScrollView.contentSize = CGSizeMake(0, (390 +85 * manyHeight ) * AutoScaleVertical);
    
}

#pragma mark tableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85*AutoScaleVertical;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.planArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PersonalPlanCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PersonalPlanCell"];
    if (cell == nil) {
        cell = [[PersonalPlanCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier: @"PersonalPlanCell"] ;
    }
    [cell relayoutCellWithModel:self.planArray[indexPath.row] andIndex:indexPath.row andArrCount:self.planArray.count andJoinNum:self.joinNumberArray[indexPath.row]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PersonDataViewController * datavc = [[PersonDataViewController alloc]init];
    datavc.model = self.model;
    PlanModel * pmodel =self.planArray[indexPath.row];
    datavc.planmodel =pmodel;
    [self.navigationController pushViewController:datavc animated:YES];
}

#pragma mark 编辑按钮方法

-(void)pressEdit{
    NSLog(@"编辑");
    //蒙版
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0.52;
    [window addSubview:_backView];
    
    self.editBabyView = [[EditBabyView alloc]init];
    [window addSubview:self.editBabyView];
    [self.editBabyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(window.mas_centerX);
        make.centerY.mas_equalTo(window.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(600*AutoScaleHorizontal, 450*AutoScaleVertical));
    }];
    
    self.editBabyView.realClassLabel = [[UILabel alloc]init];
    self.editBabyView.realClassLabel.layer.borderWidth = 1;
    self.editBabyView.realClassLabel.layer.borderColor =UIColorFromRGB(243, 243, 243).CGColor;
    [self.editBabyView.realClassLabel setTextAlignment:NSTextAlignmentCenter];
    self.editBabyView.realClassLabel.textColor = UIColorFromRGB(79, 79, 79);
    self.editBabyView.realClassLabel.font = [UIFont systemFontOfSize:18*AutoScaleHorizontal];
    [self.editBabyView addSubview:self.editBabyView.realClassLabel];
    [self.editBabyView.realClassLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.editBabyView.classLabel.mas_right).with.offset(70*AutoScaleHorizontal);
        make.centerY.mas_equalTo(self.editBabyView.classLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(250*AutoScaleHorizontal, 30*AutoScaleVertical));
    }];
    self.editBabyView.arrowView1 = [[UIImageView alloc]init];
    self.editBabyView.arrowView1.image = [UIImage imageNamed:@"arrowView"];
    [self.editBabyView addSubview:self.editBabyView.arrowView1];
    [self.editBabyView.arrowView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.editBabyView.realClassLabel.mas_right).with.offset(-15*AutoScaleHorizontal);
        make.centerY.mas_equalTo(self.editBabyView.realClassLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(10*AutoScaleHorizontal, 10*AutoScaleVertical));
    }];
    
    self.editBabyView.realClassBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.editBabyView.realClassBtn.backgroundColor = [UIColor clearColor];
    //    [self.realBirthBtn addTarget:self action:@selector(birthChoose) forControlEvents:UIControlEventTouchUpInside];
    [self.editBabyView addSubview:self.editBabyView.realClassBtn];
    [self.editBabyView.realClassBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(self.editBabyView.realClassLabel);
    }];
    
    //第一次迭代增加负责教师
    self.editBabyView.responsTeacher = [[UILabel alloc]init];
    self.editBabyView.responsTeacher.text = @"负责教师";
    self.editBabyView.responsTeacher.textColor = [UIColor blackColor];
    [self.editBabyView.responsTeacher setTextAlignment:NSTextAlignmentLeft];
    self.editBabyView.responsTeacher.font = [UIFont systemFontOfSize:18*AutoScaleHorizontal];
    [self.editBabyView addSubview:self.editBabyView.responsTeacher];
    [self.editBabyView.responsTeacher mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.editBabyView.classLabel.mas_bottom).with.offset(30*AutoScaleVertical);
        make.left.mas_equalTo(self.editBabyView.mas_left).with.offset(75*AutoScaleHorizontal);
        make.size.mas_equalTo(CGSizeMake(75*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    self.editBabyView.teacherText = [[UITextField alloc]init];
    //    self.nameText.delegate =self;
    self.editBabyView.teacherText.layer.borderWidth = 1;
    self.editBabyView.teacherText.layer.borderColor =UIColorFromRGB(243, 243, 243).CGColor;
    [self.editBabyView addSubview:self.editBabyView.teacherText];
    [self.editBabyView.teacherText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.editBabyView.responsTeacher.mas_right).with.offset(70*AutoScaleHorizontal);
        make.centerY.mas_equalTo(self.editBabyView.responsTeacher.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(250*AutoScaleHorizontal, 30*AutoScaleVertical));
    }];
    //将家长电话下移
    [self.editBabyView.phoneLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.editBabyView.responsTeacher.mas_bottom).with.offset(30*AutoScaleVertical);
        make.left.mas_equalTo(self.editBabyView.mas_left).with.offset(75*AutoScaleHorizontal);
        make.size.mas_equalTo(CGSizeMake(75*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    [self.editBabyView.PhoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.editBabyView.phoneLabel.mas_right).with.offset(70*AutoScaleHorizontal);
        make.centerY.mas_equalTo(self.editBabyView.phoneLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(250*AutoScaleHorizontal, 30*AutoScaleVertical));
    }];

    //选取班级/////////////////////////////////////////////////////////////////////////
    NSInteger taga = 0;
    self.scrollBackView = [[UIView alloc]init];
    UITapGestureRecognizer * scTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapSC)];
    [self.scrollBackView addGestureRecognizer:scTap];
    self.scrollBackView.backgroundColor = [UIColor clearColor];
    [window addSubview:self.scrollBackView];
    [self.scrollBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(self.view);
    }];
    
    self.choosClassScrollView =[[UIScrollView alloc]init];
    self.choosClassScrollView.backgroundColor = [UIColor clearColor];
    [self.scrollBackView addSubview:self.choosClassScrollView];
    [self.choosClassScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.editBabyView.realClassLabel.mas_bottom);
        make.left.mas_equalTo(self.editBabyView.realClassLabel.mas_left);
        make.size.mas_equalTo(CGSizeMake(250*AutoScaleHorizontal, 180*AutoScaleVertical));
    }];
    self.choosClassScrollView.contentSize=CGSizeMake(0, 30*self.schoolNameArr.count*AutoScaleVertical);
    for (NSString * str in self.schoolNameArr) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.layer.borderWidth =1;
        btn.layer.borderColor = UIColorFromRGB(188, 188, 188).CGColor;
        [btn setTitle:str forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:18*AutoScaleHorizontal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn.backgroundColor = UIColorFromRGB(26, 167, 157);
        btn.tag = 1000+taga;
        [btn addTarget:self action:@selector(chooseClassWithSender:) forControlEvents:UIControlEventTouchUpInside];
        [self.choosClassScrollView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.choosClassScrollView.mas_top).with.offset(taga*30*AutoScaleVertical);
            make.left.mas_equalTo(self.choosClassScrollView.mas_left);
            make.size.mas_equalTo(CGSizeMake(250*AutoScaleHorizontal, 30*AutoScaleVertical));
        }];
        
        taga++;
    }
    self.scrollBackView.hidden = YES;
//////////////////////////////////////////////////////////////////////////////

    //编辑孩子，给一个初始值
    self.editBabyView.nameText.text = self.model.ChildName;
    if ([self.model.Gender isEqual:@1]) {
        self.editBabyView.boyBtn.selected = YES;
        self.editBabyView.girlBtn.selected = NO;
    }else{
        self.editBabyView.boyBtn.selected = NO;
        self.editBabyView.girlBtn.selected = YES;
    }
    self.editBabyView.realBirthLabel.text = self.birthNameLabel.text;
    self.editBabyView.realClassLabel.text = self.model.Name;
    self.editBabyView.PhoneText.text = self.model.Phone;
    self.editBabyView.teacherText.text = self.model.ResponsibleTeacher;
    self.editBabyView.nameText.delegate  =self;
    self.editBabyView.PhoneText.delegate  =self;
    [self.editBabyView.cancelBtn addTarget:self action:@selector(pressCancel) forControlEvents:UIControlEventTouchUpInside];
    [self.editBabyView.realBirthBtn addTarget:self action:@selector(pressBirth) forControlEvents:UIControlEventTouchUpInside];
    [self.editBabyView.realClassBtn addTarget:self action:@selector(pressClass) forControlEvents:UIControlEventTouchUpInside];
    [self.editBabyView.confirmBtn addTarget:self action:@selector(pressConfirm) forControlEvents:UIControlEventTouchUpInside];
}
//取消添加
-(void)pressCancel{
    [self.editBabyView.realClassView removeFromSuperview];
    [_backView removeFromSuperview];
    [self.editBabyView removeFromSuperview];
}
//点击生日调取日历
-(void)pressBirth{
    [self.editBabyView.nameText resignFirstResponder];
    [self.editBabyView.PhoneText resignFirstResponder];
    [self.editBabyView.teacherText resignFirstResponder];
    _datePickerView = [[SDDatePickerView alloc]init];
    _datePickerView.delegate = self;
}
// 日历代理方法
-(void)cancelBtnClick{
    NSLog(@"取消日历");
}
- (void)sureBtnWithDate:(NSString *)dateStr
{
    NSLog(@"选择日历");
    self.editBabyView.realBirthLabel.text = dateStr;
}
// 选取班级下拉框
-(void)pressClass{
    [self.editBabyView.nameText resignFirstResponder];
    [self.editBabyView.PhoneText resignFirstResponder];
    [self.editBabyView.teacherText resignFirstResponder];
    self.scrollBackView.hidden = NO;
}

-(void)tapSC{
    self.scrollBackView.hidden = YES;
}
//确定选取班级
-(void)chooseClassWithSender:(UIButton*)sender{
    self.scrollBackView.hidden = YES;
    self.editBabyView.realClassLabel.text = sender.titleLabel.text;
    //    self.AddChildView.realClassView.hidden = YES;
}
//确定修改孩子
-(void)pressConfirm{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    if (self.editBabyView.nameText.text.length ==0) {
        [self showMessage:@"孩子姓名不能为空"];
    }else if (self.editBabyView.realBirthLabel.text.length==0){
        [self showMessage:@"孩子生日不能为空"];
    }else if(self.editBabyView.realClassLabel.text.length ==0){
        [self showMessage:@"孩子班级不能为空"];
    }else{
    //更新 childlist 数据源
    NSMutableArray * childlistArr = [_allDataDictionary objectForKey:@"ChildList"];
        NSMutableArray * allClassArr  = [[NSMutableArray alloc]init];
        allClassArr = [_allDataDictionary objectForKey:@"ClassList"];
    for (NSMutableDictionary * dict in childlistArr) {
        if ([[dict objectForKey:@"Id"]isEqual:self.model.Id]) {
            /////////////////////
            if (![[dict objectForKey:@"Birthday"]isEqualToString:self.editBabyView.realBirthLabel.text]) {
                [dict setObject:@"1" forKey:@"1900V"];
            }
            [dict setObject:self.editBabyView.realBirthLabel.text forKey:@"Birthday"];
            self.model.Birthday = self.editBabyView.realBirthLabel.text;
            /////////////////////
            if (![[dict objectForKey:@"ChildName"]isEqualToString:self.editBabyView.nameText.text]) {
                [dict setObject:@"1" forKey:@"1900V"];
            }
            [dict setObject:self.editBabyView.nameText.text forKey:@"ChildName"];
            self.model.ChildName = self.editBabyView.nameText.text;
            ////////////////////
            if (self.editBabyView.boyBtn.selected == YES) {
                if (![[dict objectForKey:@"Gender"]isEqual:@1]) {
                    [dict setObject:@"1" forKey:@"1900V"];
                }
                [dict setObject:@1 forKey:@"Gender"];
                self.model.Gender = @1;
            }else{
                if ([[dict objectForKey:@"Gender"]isEqual:@1]) {
                    [dict setObject:@"1" forKey:@"1900V"];
                }
                [dict setObject:@2 forKey:@"Gender"];
                self.model.Gender = @2;
            }
            /////////////////////////
            if (![[dict objectForKey:@"Name"]isEqualToString:self.editBabyView.realClassLabel.text]) {
                [dict setObject:@"1" forKey:@"1900V"];
            }
            [dict setObject:self.editBabyView.realClassLabel.text forKey:@"Name"];
            self.model.Name = self.editBabyView.realClassLabel.text;
            for (NSMutableDictionary * classdic in allClassArr) {
                if ([[classdic objectForKey:@"Name"]isEqualToString:self.editBabyView.realClassLabel.text]) {
                    [dict setObject:[classdic objectForKey:@"Id"]  forKey:@"ClassId"];
                    self.model.ClassId = [classdic objectForKey:@"Id"];
                }
            }
            /////////////////////////
            if (![[dict objectForKey:@"ResponsibleTeacher"]isEqualToString:self.editBabyView.teacherText.text]) {
                [dict setObject:@"1" forKey:@"1900V"];
            }
            [dict setObject:self.editBabyView.teacherText.text forKey:@"ResponsibleTeacher"];
            self.model.ResponsibleTeacher = self.editBabyView.teacherText.text;
            /////////////////////////
            if (![[dict objectForKey:@"Phone"]isEqualToString:self.editBabyView.PhoneText.text]) {
                [dict setObject:@"1" forKey:@"1900V"];
            }
            [dict setObject:self.editBabyView.PhoneText.text forKey:@"Phone"];
            self.model.Phone = self.editBabyView.PhoneText.text;
        }
    }
    //更新master 数据源
        NSMutableArray * resultArr = [_allDataDictionary objectForKey:@"TestResult"];
        for (NSMutableDictionary * dict in resultArr) {
            NSMutableDictionary * masterDic = [dict objectForKey:@"master"];
            if ([[masterDic objectForKey:@"ChildId"]isEqual:self.model.Id]) {
                [masterDic setObject:self.editBabyView.nameText.text forKey:@"ChildName"];
                if (self.editBabyView.boyBtn.selected == YES) {
                [masterDic setObject:@1 forKey:@"Gender"];
                }else{
                [masterDic setObject:@2 forKey:@"Gender"];
                }
                [masterDic setObject:self.editBabyView.realBirthLabel.text forKey:@"Birthday"];
                for (NSMutableDictionary * classdic in allClassArr) {
                    if ([[classdic objectForKey:@"Name"]isEqualToString:self.editBabyView.realClassLabel.text]) {
                        [masterDic setObject:[classdic objectForKey:@"Id"]  forKey:@"ClassId"];
                    }
                }
                [masterDic setObject:self.editBabyView.PhoneText.text forKey:@"Phone"];
                [masterDic setObject:self.editBabyView.realClassLabel.text forKey:@"ClassName"];
            }
        }
        
        
        [_allDataDictionary removeObjectForKey:@"ChildList"];
        [_allDataDictionary setObject:childlistArr forKey:@"ChildList"];
        [_allDataDictionary removeObjectForKey:@"TestResult"];
        [_allDataDictionary setObject:resultArr forKey:@"TestResult"];
        NSData * data = [NSJSONSerialization dataWithJSONObject:_allDataDictionary options:NSJSONWritingPrettyPrinted error:nil];
        NSString * allStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        [user removeObjectForKey:@"AllData"];
        [user setObject:allStr forKey:@"AllData"];
        [user synchronize];
    
        //更新页面
       self.birthNameLabel.text  = self.editBabyView.realBirthLabel.text;
       self.phoneNameLabel.text = self.editBabyView.PhoneText.text;
        self.classNameLabel.text = self.editBabyView.realClassLabel.text;
        self.topNameLabel.text = self.editBabyView.nameText.text;
        self.responseTeacher.text = self.editBabyView.teacherText.text;
        if (self.editBabyView.boyBtn.selected ==YES) {
            self.genderIMG.image = [UIImage imageNamed:@"manIMGS"];
        }else{
            self.genderIMG.image = [UIImage imageNamed:@"womenIMGS"];
        }
    [_backView removeFromSuperview];
    [self.editBabyView removeFromSuperview];
    }
}

#pragma mark 左右导航栏按钮方法
-(void)leftBarBtnClicked{
    NSLog(@"左按钮");
    [self.navigationController popViewControllerAnimated:YES];
}
//删除孩子
-(void)rightBarBtnClicked{
    NSLog(@"右按钮");
    //蒙版
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0.52;
    [window addSubview:_backView];
    
    self.alertView = [[BaseAlertView alloc]init];
    [window addSubview:self.alertView];
    [self.alertView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(window.mas_centerX);
        make.centerY.mas_equalTo(window.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(500*AutoScaleHorizontal, 250*AutoScaleVertical));
    }];
    self.alertView.infomationLabel.text = @"确认删除该学员？";
    
    [self.alertView.yesBtn addTarget:self action:@selector(pressAlertYes) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView.noBtn addTarget:self action:@selector(pressAlertNo) forControlEvents:UIControlEventTouchUpInside];
}
-(void)pressAlertYes{
    NSLog(@"确定删除");
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    NSMutableArray * childlistArr = [_allDataDictionary objectForKey:@"ChildList"];
    for (NSMutableDictionary * dict in childlistArr.reverseObjectEnumerator) {
        if ([[dict objectForKey:@"Id"]isEqual:self.model.Id]) {
            [childlistArr removeObject:dict];
        }
    }
    //更新本地数据
    [_allDataDictionary removeObjectForKey:@"ChildList"];
    [_allDataDictionary setObject:childlistArr forKey:@"ChildList"];
    NSData * data = [NSJSONSerialization dataWithJSONObject:_allDataDictionary options:NSJSONWritingPrettyPrinted error:nil];
    NSString * allStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    [user removeObjectForKey:@"AllData"];
    [user setObject:allStr forKey:@"AllData"];
    [user setObject:@"yes" forKey:@"ValueChanged"];
    [user synchronize];
    
    if (self.model.Id>0) {
        
        if ([user objectForKey:@"deleteChildArray"]!=nil) {
        NSString* str = [user objectForKey:@"deleteChildArray"];
        //    NSLog(@"eee===%@",str);
        NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSMutableDictionary * dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray * allClassArr  = [[NSMutableArray alloc]init];
        allClassArr = [dict objectForKey:@"deleteChildArray"];
        [allClassArr addObject:self.model.Id];
        
        [dict removeObjectForKey:@"deleteChildArray"];
        [dict setObject:allClassArr forKey:@"deleteChildArray"];
        NSData * data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
        NSString * allStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        [user removeObjectForKey:@"deleteChildArray"];
        [user setObject:allStr forKey:@"deleteChildArray"];
        [user synchronize];
        }else{
            NSMutableArray * DCA = [[NSMutableArray alloc]init];
            [DCA addObject:self.model.Id];
            NSMutableDictionary * dcd = [[NSMutableDictionary alloc]init];
            [dcd setObject:DCA forKey:@"deleteChildArray"];
            NSData * data = [NSJSONSerialization dataWithJSONObject:dcd options:NSJSONWritingPrettyPrinted error:nil];
            NSString* deleteChild = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            [user setObject:deleteChild forKey:@"deleteChildArray"];
            [user synchronize];
        }
        
    }
    
    
    [_backView removeFromSuperview];
    [self.alertView removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
    [self showMessage:@"删除成功"];
}
-(void)pressAlertNo{
    NSLog(@"取消删除");
    [_backView removeFromSuperview];
    [self.alertView removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
